package com.example.itcacp.service;

import com.example.itcacp.entity.Project;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProjectRecommendationService {

    @Resource
    private DataPreparationService dataPreparationService;

    @Resource
    private RecommendationGenerator recommendationGenerator;

    @Resource
    private ProjectService projectService;

    /**
     * 基于浏览记录生成项目推荐
     * @param userId 目标用户ID
     * @param k 最相似用户数量
     * @param limit 推荐结果数量限制
     * @return 推荐项目列表
     */
    public List<Project> generateProjectRecommendations(Long userId, int k, int limit) {
        // 构建基于浏览记录的用户-内容矩阵
        Map<Long, Map<Long, Integer>> userItemMatrix = dataPreparationService.buildUserBrowseMatrix();

        // 生成推荐内容ID列表
        List<Long> recommendedContentIds = recommendationGenerator.generateRecommendations(
                userItemMatrix, userId, k);

        // 查询推荐的项目
        List<Project> recommendedProjects = new ArrayList<> ();
        for (Long contentId : recommendedContentIds) {
            Project project = projectService.getProjectById(contentId);
            if (project != null) {
                recommendedProjects.add(project);
                if (recommendedProjects.size() >= limit) {
                    break;
                }
            }
        }

        return recommendedProjects;
    }
}
